<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="字典标签" prop="label">
      <el-input v-model="dataForm.label" placeholder="字典标签"></el-input>
    </el-form-item>
    <el-form-item label="字典值" prop="value">
      <el-input v-model="dataForm.value" placeholder="字典值"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="dictSort">
      <el-input v-model="dataForm.dictSort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          detailId: 0,
          dictId: '',
          label: '',
          value: '',
          dictSort: '',
          createUserName: '',
          updateUserName: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          dictId: [
            { required: true, message: '字典id不能为空', trigger: 'blur' }
          ],
          label: [
            { required: true, message: '字典标签不能为空', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '字典值不能为空', trigger: 'blur' }
          ],
          dictSort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,dictId) {
        console.log("cit",dictId);
        this.dataForm.dictId=dictId
        this.dataForm.detailId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.detailId) {
            this.$http({
              url: this.$http.adornUrl(`upms/sysdictdetail/info/${this.dataForm.detailId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 1) {
                this.dataForm.dictId = data.data.dictId
                this.dataForm.label = data.data.label
                this.dataForm.value = data.data.value
                this.dataForm.dictSort = data.data.dictSort
                this.dataForm.createUserName = data.data.createUserName
                this.dataForm.updateUserName = data.data.updateUserName
                this.dataForm.createTime = data.data.createTime
                this.dataForm.updateTime = data.data.updateTime 
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`upms/sysdictdetail/${!this.dataForm.detailId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'detailId': this.dataForm.detailId || undefined,
                'dictId': this.dataForm.dictId,
                'label': this.dataForm.label,
                'value': this.dataForm.value,
                'dictSort': this.dataForm.dictSort,
                'createUserName': this.dataForm.createUserName,
                'updateUserName': this.dataForm.updateUserName,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 1) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
